﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;

namespace TeighaHelper.Core
{
    public class EntityReader
    {
        public static CADGeometryPolyLine polyLineFromEntity(Entity entity)
        {    
            //  List<CADGeometryPolyLine> listPolylines = new List<CADGeometryPolyLine>();
            Polyline pl = (Polyline)entity;
            IList<PointLoc> listPoints = new List<PointLoc>();
            for (int i = 0; i < pl.NumberOfVertices; i++)
            {
                listPoints.Add(new PointLoc(pl.GetPoint2dAt(i).X.MilliToFeet(), pl.GetPoint2dAt(i).Y.MilliToFeet(), 0));
            }

            //PolyLine polyLine = PolyLine.Create(listPoints);
            //listCADModels.Add(new CADGeometryModel() { CadPolyLine = polyLine, LayerName = pl.Layer });

            CADGeometryPolyLine geoPoly = new CADGeometryPolyLine();
            geoPoly.pointLocs = listPoints.ToList();
            geoPoly.LayerName = entity.Layer;
            //listPolylines.Add(geoPoly);

            return geoPoly;
        }


        public static CADGeometryLine lineFromEntity(Entity entity)
        {
           // List<CADGeometryLine> listLines = new List<CADGeometryLine>();

            Line line = (Line)entity;
            //Autodesk.Revit.DB.Line revitLine = Autodesk.Revit.DB.Line.CreateBound(ConverCADPointToRevitPoint(line.StartPoint), ConverCADPointToRevitPoint(line.EndPoint));
            //listCADModels.Add(new CADGeometryModel() { CadCurve = revitLine as Autodesk.Revit.DB.Curve, LayerName = line.Layer });

            CADGeometryLine geoLine = new CADGeometryLine();
            geoLine.stp = line.StartPoint.Point3dToPointLoc();
            //Point3d newStp = line.StartPoint / 304.8;
            // geoLine.stpX = newStp.X; geoLine.stpY = newStp.Y; geoLine.stpZ = newStp.Z;
            geoLine.edp = line.EndPoint.Point3dToPointLoc();
            //Point3d newEdp = line.EndPoint / 304.8;
            //geoLine.edpX = newEdp.X; geoLine.edpY = newEdp.Y; geoLine.edpZ = newEdp.Z;
            geoLine.LayerName = line.Layer;
            //listLines.Add(geoLine);
            return geoLine;
        }

        public static CADGeometryArc arcFromEntity(Entity entity)
        {
            //List<CADGeometryArc> listArcs = new List<CADGeometryArc>();

            Arc arc = (Arc)entity;
            double enda, stara;
            if (arc.StartAngle > arc.EndAngle)
            {
                enda = arc.StartAngle;
                stara = arc.EndAngle;
            }
            else
            {
                enda = arc.EndAngle;
                stara = arc.StartAngle;
            }
            //Autodesk.Revit.DB.Arc revitArc = Autodesk.Revit.DB.Arc.Create(Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(new XYZ(arc.Normal.X, arc.Normal.Y, arc.Normal.Z),
            //    ConverCADPointToRevitPoint(arc.Center)), MillimetersToUnits(arc.Radius), stara, enda);

            CADGeometryArc geoArc = new CADGeometryArc();
            geoArc.normal = arc.Normal.Vector3dToPointLoc();
            //geoArc.normalX = arc.Normal.X; geoArc.normalY = arc.Normal.Y; geoArc.normalZ = arc.Normal.Z;                                                     
            geoArc.center = arc.Center.Point3dToPointLoc();
            //Point3d newCenter = arc.Center / 304.8;
            // geoArc.centerX = newCenter.X; geoArc.centerY = newCenter.Y; geoArc.centerZ = newCenter.Z;
            geoArc.redius = arc.Radius.MilliToFeet();
            geoArc.startAngle = stara;
            geoArc.endAngle = enda;
            geoArc.LayerName = arc.Layer;
            // listArcs.Add(geoArc);
            return geoArc;
        }


        public static CADDbTextModel dbTextFromEntity(Entity entity)
        { 
            List<CADDbTextModel> listCADModels = new List<CADDbTextModel>();
            CADDbTextModel model = new CADDbTextModel();

            DBText text = (DBText)entity;
            // model.Location = ConverCADPointToRevitPoint(text.Position);
            model.LocX = text.Position.X / 304.8;
            model.LocY = text.Position.Y / 304.8;
            model.LocZ = text.Position.Z / 304.8;
            model.Text = text.TextString;
            model.Angel = text.Rotation;
            model.Layer = text.Layer;
          //  listCADModels.Add(model);

            return model;
        }

        public static CADMTextModel mTextFromEntity(Entity entity)
        {
            // List<CADMTextModel> listCADModels = new List<CADMTextModel>();
            CADMTextModel model = new CADMTextModel();
            MText mText = (MText)entity;
            // model.Location = ConverCADPointToRevitPoint(mText.Location);
            model.LocX = mText.Location.X / 304.8;
            model.LocY = mText.Location.Y / 304.8;
            model.LocZ = mText.Location.Z / 304.8;
            model.Text = mText.Text;
            model.Angel = mText.Rotation;
            model.Layer = mText.Layer;
           // listCADModels.Add(model);
            return model;
        }


        
        public static List<string[]> GetXData(Entity ent)
        {
            List<string[]> xDataLst = new List<string[]>();

            ResultBuffer resultBuffer = ent.XData;
            if (resultBuffer != null)
            {
                TypedValue[] typedValues = resultBuffer.AsArray();
                foreach (TypedValue typedValue in typedValues)
                { 
                    string typeCode = typedValue.TypeCode.ToString();
                    string value = typedValue.Value.ToString(); 
                    xDataLst.Add(new string[] { typeCode, value });
                    //xDataLst.Add(new string[] { typeCode, value });
                }
            }

            return xDataLst;

        }

    }
}
